﻿/*******************************************************************************
 * CreateTime:2022-04-04 11:37:44
 * Description: YoursLC有源低代码
*********************************************************************************/

using System;
using System.Data;
using System.Collections.Generic;
using Think9.Models;
using Think9.Services.Base;
using Think9.Services.Basic;
using Think9.Services.Table;
using Think9.Services.Flow;

namespace Think9.Services.CodeBuild
{
	/// <summary>
	/// 主表Service
	/// </summary>
	public class Demo25Service : BaseService<Demo25Model>
	{
		private ComService ComService = new ComService();
		private string split = BaseUtil.ComSplit;//字符分割 用于多选项的分割等 如split+选项1+split+选项2+split
		private string sql;

		//添加前数据处理
		public int BeforeAdd(ref string err, CurrentUserEntity CurrentUser, CurrentPrcsEntity mPrcs, string flowid, string maintbid, string tbname)
		{
			int listid = 0;

			//处理自动编号-- 如果有则添加一条数据,返回自增长的listid;如果没有则返回0
			listid = AutoNo.SetAutoNumber(ref err, CurrentUser, mPrcs, flowid, tbname);



			return listid;
		}

		//编辑后数据处理
		public string AfterEdit(string listid, string flowid, string prcsid, CurrentUserEntity user)
        {
			string err = "";

			//调用通用函数实现数据回写，用户可自定义 数据回写在录入表管理/数据读写中设置
			WriteBack WriteBack = new WriteBack();
			string maintbid = flowid.Replace("bi_", "tb_").Replace("fw_", "tb_");
			err = WriteBack.WriteValueByRelationSet(listid, flowid, maintbid, prcsid, user);

			return err;
		}

		//完成后数据处理
		public string AfterFinish(string listid, string flowid, string prcsid, CurrentUserEntity user)
		{
			string err = AfterEdit(listid, flowid, prcsid, user);
			if(string.IsNullOrEmpty(err))
            {
				//数据锁定等
				FlowCom.FinishFlowPrcs(listid, flowid, prcsid, user);
            }

			return err;
		}

		/// <summary>
		/// 主表查询调用 返回列表 查询条件可在录入表指标属性中设置
		/// </summary>
		/// <param name="total"></param>
		/// <param name="model"></param>
		/// <param name="pageInfo"></param>
		/// <param name="user"></param>
		/// <param name="flowid"></param>
		/// <param name="tbid"></param>
		/// <returns></returns>
		public IEnumerable<dynamic> GetPageListBySearch(ref long total, Demo25Model model, PageInfoEntity pageInfo, CurrentUserEntity user, string flowid, string tbid)
		{
			string where = BasicHelp.GetWhereByFlowId(user, flowid);

			//表1v3和
			if(model.inB1V3H != null)
			{
				where += " and (tb_Demo25.inB1V3H >= @inB1V3H)"; 
			}
			if(model.inB1V3H_Exa  != null)
			{
				where += " and (tb_Demo25.inB1V3H <= @inB1V3H_Exa)"; 
			}
			//表1v3最大
			if(model.inB1V3ZD != null)
			{
				where += " and (tb_Demo25.inB1V3ZD >= @inB1V3ZD)"; 
			}
			if(model.inB1V3ZD_Exa  != null)
			{
				where += " and (tb_Demo25.inB1V3ZD <= @inB1V3ZD_Exa)"; 
			}
			//表2v1和
			if(model.inB2V1H != null)
			{
				where += " and (tb_Demo25.inB2V1H >= @inB2V1H)"; 
			}
			if(model.inB2V1H_Exa  != null)
			{
				where += " and (tb_Demo25.inB2V1H <= @inB2V1H_Exa)"; 
			}
			//表二v2最小
			if(model.inBEV2ZX != null)
			{
				where += " and (tb_Demo25.inBEV2ZX >= @inBEV2ZX)"; 
			}
			if(model.inBEV2ZX_Exa  != null)
			{
				where += " and (tb_Demo25.inBEV2ZX <= @inBEV2ZX_Exa)"; 
			}
			//两者和
			if(model.inLZH != null)
			{
				where += " and (tb_Demo25.inLZH >= @inLZH)"; 
			}
			if(model.inLZH_Exa  != null)
			{
				where += " and (tb_Demo25.inLZH <= @inLZH_Exa)"; 
			}
			//复杂的表达
			if(model.inFZDBD != null)
			{
				where += " and (tb_Demo25.inFZDBD >= @inFZDBD)"; 
			}
			if(model.inFZDBD_Exa  != null)
			{
				where += " and (tb_Demo25.inFZDBD <= @inFZDBD_Exa)"; 
			}
			return base.GetPageByFilter(ref total, model, pageInfo, where);

		}

		/// <summary>
		/// 主表 为下拉选择、多选、单选准备动态数据源
		/// 负责列表显示时value与text转化 动态数据源来源于为指标指定的数据规范
		/// </summary>
		/// <param name="from"></param>
		/// <param name="param"></param>
		/// <returns></returns>
		public IEnumerable<valueTextEntity> GetSelectList(string from, object param)
		{
			DataTable dt = DataTableHelp.NewValueTextDt();


			//DataTable转换成IEnumerable
			return DataTableHelp.ToEnumerable<valueTextEntity>(dt);
		}

		/// <summary>
		/// 弹出页面的查询条件有下拉选择、多选、单选时，为其绑定动态数据源
		/// 动态数据源来源于为指标指定的数据规范
		/// </summary>
		/// <param name="tbid"></param>
		/// <param name="indexid"></param>
		/// <param name="from"></param>
		/// <param name="param"></param>
		/// <returns></returns>
		public IEnumerable<valueTextEntity> GetSelectList(string tbid, string indexid, string from, object param)
		{
			DataTable dt = DataTableHelp.NewValueTextDt();

			//主表指标弹出页面
			if (tbid == "_main")
			{

			}
			else//子表指标弹出页面
			{

			}

			//DataTable转换成IEnumerable
			return DataTableHelp.ToEnumerable<valueTextEntity>(dt);
		}

		/// <summary>
		/// 数据删除
		/// </summary>
		/// <param name="id"></param>
		/// <param name="fwid"></param>
		/// <returns></returns>
		public bool DeleteByID(string id, string fwid)
		{
			//删除子表数据
			ComService.ExecuteSql("delete from tb_Formula01 where ListId=" + id + "");
			ComService.ExecuteSql("delete from tb_Formula02 where ListId=" + id + "");
			AttachmentService.DelAttachment(int.Parse(id), fwid);//删除附件 要最先执行
			//删除flowrunlist、flowrunprcslist中关联数据
			if (fwid.StartsWith("fw_"))
			{
				ComService.ExecuteSql("delete from flowrunlist where ListId = " + id + "");
				ComService.ExecuteSql("delete from flowrunprcslist where ListId = " + id + "");
			}

			AutoNo.DelAutoNo(id, fwid);//删除自动编号

			return base.DeleteByWhere("where ListId=" + id + "");//删除主表数据
		}

		/// <summary>
		///返回可修改字段 自由流程排除了隐藏指标 固定流程排除了隐藏指标及不可写字段
		/// </summary>
		/// <param name="prcno"></param>
		/// <returns></returns>
		public string GetUpdateFields(string prcno)
		{
			//自由流程或无流程排除了隐藏指标
			 return "inB1V3H,inB1V3ZD,inB2V1H,inBEV2ZX,inLZH,inFZDBD";
		}

		//主表计算式 根据录入表指标计算式生成
		public void GetCalculationValue(ref Demo25Model model, int listid)
		{
			model.inB1V3H = ExtConvert.ToDecimalOrNull(ComService.GetSingle("select SUM(v3) from tb_Formula01 where ListId = " + listid + ""));
			model.inB1V3ZD = ExtConvert.ToDecimalOrNull(ComService.GetSingle("select MAX(v3) from tb_Formula01 where ListId = " + listid + ""));
			model.inB2V1H = ExtConvert.ToDecimalOrNull(ComService.GetSingle("select SUM(v1) from tb_Formula02 where ListId = " + listid + ""));
			model.inBEV2ZX = ExtConvert.ToDecimalOrNull(ComService.GetSingle("select MIN(v1) from tb_Formula02 where ListId = " + listid + ""));
			model.inLZH = model.inB1V3H + model.inB1V3ZD;
			model.inFZDBD = (ExtConvert.ToIntOrNull(ComService.GetSingle("select COUNT(*) from tb_Formula01 where ListId = " + listid + "")) + ExtConvert.ToIntOrNull(ComService.GetSingle("select COUNT(*) from tb_Formula02 where ListId = " + listid + ""))) * model.inB2V1H + model.inBEV2ZX;
		}
	}
}